package io.aircraft.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.aircraft.common.page.PageData;
import io.aircraft.common.utils.ConvertUtils;
import io.aircraft.common.utils.ProcResult;
import io.aircraft.modules.sys.dto.SysUserDTO;
import io.aircraft.modules.sys.service.SysUserService;
import io.aircraft.modules.wms.dao.TaskDao;
import io.aircraft.modules.wms.dto.TaskAffirmDTO;
import io.aircraft.modules.wms.dto.TaskDTO;
import io.aircraft.modules.wms.entity.stored.ProcTaskAffirmEntity;
import io.aircraft.modules.wms.entity.TaskEntity;
import io.aircraft.common.service.impl.CrudServiceImpl;
import io.aircraft.modules.wms.service.StoredProcService;
import io.aircraft.modules.wms.service.TaskService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 任务记录
 *
 * @author Cycle gengruyi01@163.com
 * @since 1.0.0 2022-10-13
 */
@Service
public class TaskServiceImpl extends CrudServiceImpl<TaskDao, TaskEntity, TaskDTO> implements TaskService {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private StoredProcService storedProcService;

    @Override
    public PageData<TaskDTO> page(Map<String, Object> params) {
        IPage<TaskEntity> page = baseDao.selectPage(
                getPage(params, "update_date", false),
                getWrapper(params));
        //获取[sys_user]的信息
        List<SysUserDTO> userList = sysUserService.getList();
        //将用户编号填充至creatorName&updaterName
        page.getRecords().forEach(entity -> {
            entity.setCreatorName(sysUserService.getUsernameById(entity.getCreator(), userList));
            entity.setUpdaterName(sysUserService.getUsernameById(entity.getUpdater(), userList));
        });
        return getPageData(page, TaskDTO.class);
    }

    @Override
    public QueryWrapper<TaskEntity> getWrapper(Map<String, Object> params) {
        String contNo = (String) params.get("contNo");
        String mtNo = (String) params.get("mtNo");
        String originPosition = (String) params.get("originPosition");
        String targetPosition = (String) params.get("targetPosition");
        String currentPosition = (String) params.get("currentPosition");
        String dtId = (String) params.get("dtId");
        String stockId = (String) params.get("stockId");
        String spuNo = (String) params.get("spuNo");
        String lotNo = (String) params.get("lotNo");
        String taskType = (String) params.get("taskType");
        String taskSubType = (String) params.get("taskSubType");
        String startDate = (String) params.get("startDate");
        String endDate = (String) params.get("endDate");

        QueryWrapper<TaskEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(contNo), "origin_cont_no", contNo);
        wrapper.eq(StringUtils.isNotBlank(mtNo), "mt_no", mtNo);
        wrapper.eq(StringUtils.isNotBlank(dtId), "dt_id", dtId);
        wrapper.eq(StringUtils.isNotBlank(stockId), "stock_id", stockId);
        wrapper.eq(StringUtils.isNotBlank(taskType), "task_type", taskType);
        wrapper.eq(StringUtils.isNotBlank(taskSubType), "task_sub_type", taskSubType);
        wrapper.eq(StringUtils.isNotBlank(lotNo), "lot_no", lotNo);
        wrapper.like(StringUtils.isNotBlank(spuNo), "spu_no", spuNo);
        wrapper.like(StringUtils.isNotBlank(originPosition), "origin_position", originPosition);
        wrapper.like(StringUtils.isNotBlank(targetPosition), "target_position", targetPosition);
        wrapper.like(StringUtils.isNotBlank(currentPosition), "current_position", currentPosition);
        wrapper.ge(StringUtils.isNotBlank(startDate), "create_date", startDate);
        wrapper.le(StringUtils.isNotBlank(endDate), "create_date", endDate);

        return wrapper;
    }

    /**
     * 出库确认
     *
     * @param dto 待确认任务
     * @return 结果
     */
    @Override
    public ProcResult affirm(TaskAffirmDTO dto) {
        ProcTaskAffirmEntity parameters = ConvertUtils.sourceToTarget(dto, ProcTaskAffirmEntity.class);
        return storedProcService.affirmByTask(parameters);
    }

    @Override
    public int batchSave(List<TaskEntity> taskList) {
        return baseDao.batchInsert(taskList);
    }
}